import React, { Component } from 'react'

export default class Child extends Component {

  state={
    time:new Date().toLocaleTimeString()
  }

  render() {
    // console.log('child render')
    return (
      <div>
       
       父组件传过来的属性： {
          this.props.num
        } <br></br>
        当前组件的状态：
        {
          this.state.num
        }

        <br></br>
        {
          this.state.time
        }
        <button onClick={
          ()=>{
            this.setState({
              num:this.state.num +1
            })
          }
        }>修改子组件数据</button>
      </div>
    )
  }

  static getDerivedStateFromProps(props,state){
     console.log('getDrivedStateFromProps');
     console.log(props)
     if(state.num > 1){
       return null
     }
     return {
       ...props
     }
   }


  componentWillUnmount(){
    console.log('child componentWillUnmount')
    this.timer && clearInterval(this.timer)
  }

  componentDidMount(){
    // this.timer = setInterval(() => {
    //   this.setState({
    //     time:new Date().toLocaleTimeString()
    //   })
    // }, 1000);
  }

}
